package com.wframe.project.former.dataservices.marketing;

import com.frame.database.JBaseDao;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.util.DateConvert;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/14.
 */
public class FullGiftsDao extends JBaseDao {

    public FullGiftsDao(){
        super("wb_promotion_purchase_gift");
    }
    public PageResult queryFullGiftsList(JdataBean parms, int size, int pageno) throws Exception{
        String sql="SELECT t.id,t.platform,t. NAME productname,t.sort_value,t.start_time,t.end_time,t.state,t.product_type,CASE product_type WHEN '0' THEN '全部商品' ELSE '部分商品' END goods,(SELECT c. NAME FROM wb_products_class c WHERE t.promotion_type = 3 AND product_type = 3 AND t.product_val = c.id) classname,UNIX_TIMESTAMP(t.start_time) startmsec,UNIX_TIMESTAMP(t.end_time) endmsec,UNIX_TIMESTAMP() newtime FROM wb_promotions t WHERE t.promotion_type=3 and(state=1 or state=0)";
        List parmlist = new ArrayList();
        if (parms != null){
            if (StringUtils.isNotBlank(parms.getString("name"))) {
                sql += " and t.name like ?";
                parmlist.add("%" + parms.getString("name") + "%");
            }
            if (StringUtils.isNotBlank(parms.getString("platform"))) {
                sql += " and t.platform=?";
                parmlist.add(parms.getString("platform"));
            }
            if (StringUtils.isNotBlank(parms.getString("start"))) {
                sql += " and t.start_time>=?";
                parmlist.add(DateConvert.toDate(parms.getString("start")+" 00:00:00"));
            }
            if (StringUtils.isNotBlank(parms.getString("end"))) {
                sql += " and t.start_time<=?";
                parmlist.add(DateConvert.toDate(parms.getString("end")+" 23:59:59"));
            }
            //已暂停
            if (StringUtils.isNotBlank(parms.getString("state")) && parms.getString("state").equals("0")){
                sql +=" and state=0 and start_time<now() and end_time>NOW()";
            }
            //未开始
            if (StringUtils.isNotBlank(parms.getString("state")) && parms.getString("state").equals("1")){
                sql +=" and state=1 and start_time>now()";
            }
            //进行中
            if (StringUtils.isNotBlank(parms.getString("state")) && parms.getString("state").equals("2")){
                sql +=" and state =1 AND start_time<now() and end_time>NOW() ";
            }
            //已失效
            if (StringUtils.isNotBlank(parms.getString("state")) && parms.getString("state").equals("3")){
                sql +=" and (state =1 or state=0) and  end_time<NOW()";
            }
        }
        sql += " order by t.sort_value desc,t.id desc";
        return getSession().query2(sql, parmlist, size, pageno);
    }

    public String addControlsalesSingLe(JdataBean bean) throws Exception {
        return getSession().saveReturnKey("wb_promotion_controlsale", bean.getDataMap());
    }

    public void deleteControlsalesAll(String promotionid) throws HibernateException, Exception{
        getSession().delete("delete from wb_promotion_controlsale where promotion_id=?",new Object[]{promotionid});
    }

}
